<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad
	} from "@dcloudio/uni-app"
	import LjLayout from "@/pages_nbst/components/lj-layout.vue"
	import {
		getExamReq,
		checkExamReq,
		getPageQuestionReq
	} from "@/pages_nbst/api/exam.js"

	const type = ref(-1)
	onLoad((query) => {
		console.log('eoexam onLoad', query);
		type.value = Number(query.type)
		if (type.value === 0) {
			uni.setNavigationBarTitle({
				title: "出门考"
			})
		} else if (type.value === 1) {
			uni.setNavigationBarTitle({
				title: "进门考"
			})
		}
		getExamReq(type.value).then(res => {
			list.value = res
		})
	})
	const list = ref([])
	const onClickGoExam = (item) => {

		const nowDate = new Date().valueOf()
		const startDate = new Date(item.start.replace(/-/g, '/')).valueOf()
		const endDate = new Date(item.end.replace(/-/g, '/')).valueOf()

		if (nowDate < startDate) {
			uni.showModal({
				title: '温馨提示',
				content: '还未到考试时间，请耐心等待',
				showCancel: false
			});
			return
		} else if (nowDate > endDate) {
			uni.showModal({
				title: '温馨提示',
				content: '考试已经结束。',
				showCancel: false
			});
			return
		}
		checkExamReq(item.id, {
			pageId: item.pages[0].page_id,
			subjectId: item.pages[0].subject_id
		}).then(res => {
			if (res?.id) {
				uni.navigateTo({
					url: '/pages_nbst/views/my/userpageexam?id=' + res.id
				})
			} else {
				uni.navigateTo({
					url: '/pages_nbst/views/home/exam?pageId=' + item.pages[0].page_id +
						'&examId=' + item.id +
						'&subjectId=' + item.pages[0].subject_id
				})
			}
		})
	}
	const checkExam = (examId, pageId) => {
		console.log(examId, pageId)
	}
</script>

<template>
	<lj-layout>
		<view style="padding: 60rpx 30rpx;display: flex;flex-direction: column;gap: 30rpx;">
			<view v-if="list.length === 0">暂无考试！！</view>
			<view class="excam-box" v-for="(item, index) in list" :key="index" @click="onClickGoExam(item)">
				<image class="b-icon"
					:src="index % 2 === 0 ? '/pages_nbst/static/images/home/ellipse1.png': '/pages_nbst/static/images/home/ellipse2.png'"
					mode="widthFix" />
				<view style="margin-left: 20rpx;">
					<view style="font-size: 40rpx;">{{item.label}}</view>
					<view style="font-size: 20rpx;">考试时间：{{item.start}} ~ {{item.end}}</view>
				</view>
			</view>
		</view>
	</lj-layout>
</template>

<style scoped lang="scss">
	.excam-box {
		display: flex;
		align-items: center;
		border-radius: 10rpx;
		background-color: white;
		box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.1);
		padding: 20rpx 0;

		.b-icon {
			width: 16rpx;
			height: auto;
		}
	}
</style>